package com.yunkazaixian.syntelecom.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunkazaixian.syntelecom.entity.McProductNorms;
import com.yunkazaixian.syntelecom.entity.McSimDx;
import com.yunkazaixian.syntelecom.mapper.McProductNormsMapper;
import com.yunkazaixian.syntelecom.mapper.McSimDxMapper;
import com.yunkazaixian.syntelecom.service.DXService;
import com.yunkazaixian.syntelecom.utils.DateUtils;
import com.yunkazaixian.syntelecom.utils.ListUtils;
import com.yunkazaixian.syntelecom.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@EnableScheduling   // 1.开启定时任务
@RequestMapping("/syn/day")
public class DaySynController {

    @Autowired
    McSimDxMapper simDxMapper;

    @Autowired
    DXService dxService;


    @Autowired
    McProductNormsMapper mcProductNormsMapper;

    /**
     * 扫描前一天到期的卡，推荐时间凌晨1点左右
     *
     * @return
     */
    @RequestMapping("/disabledNumberService")
    @ResponseBody
    public Object disabledNumberService() {
        StringBuilder stringBuilder = new StringBuilder();
        LambdaQueryWrapper<McSimDx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String date = DateUtils.dateToStr(DateUtils.addDay(new Date(), -1));
        lambdaQueryWrapper.eq(McSimDx::getArriveTime, date);
        List<McSimDx> simDxList = simDxMapper.selectList(lambdaQueryWrapper);
        for (McSimDx s : simDxList) {

//            if ("1796087512172687361".equals(s.getAgentId())) {//中瑞
//                if ("K98628".equals(s.getSku())) {//300G
//
//                } else {
//                    continue;//不停机
//                }
//            }
            if ("000000".equals(s.getTenantId())) {
                continue;//不停机
            }
            try {
                dxService.disabledNumberService(s);
                dxService.disabledNumberService2(s);
                stringBuilder.append(s.getTelephone());
                stringBuilder.append(",");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 激活时间 凌晨一点左右
     *
     * @return
     */
    @RequestMapping("/testActiveSIM")
    @ResponseBody
    //@Scheduled(cron = "0/5 * * * * *")
    public Object testActiveSIM() {
        LambdaQueryWrapper<McSimDx> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(it -> it.isNotNull(McSimDx::getBatchNo).ne(McSimDx::getBatchNo, ""));//已分卡
        lambdaQueryWrapper.and(qw -> {//未激活
            qw.eq(McSimDx::getActiveTime, "").or().isNull(McSimDx::getActiveTime);
        });

        List<McSimDx> simDxList = simDxMapper.selectList(lambdaQueryWrapper);
        for (McSimDx sim : simDxList) {
            if ("1796087512172687361".equals(sim.getAgentId())) {
                continue;
            }
            Date today = new Date();
            Date quietActiveDay = DateUtils.addMonth(DateUtils.strToDate(sim.getBatchNo().substring(0, 8)), sim.getQuietPeriod());
            if (DateUtils.compare(today, quietActiveDay) > 0) {
                sim.setActiveTime(DateUtils.dateToStr(today));
                QueryWrapper qw = new QueryWrapper();
                qw.eq("norm.del_flag", 0);
                qw.eq("norm.sku", sim.getSku());
                List<McProductNorms> normList = mcProductNormsMapper.selectProduct(qw);
                if (!ListUtils.isEmpty(normList)) {
                    McProductNorms norm = normList.get(0);
                    if (norm != null) {
                        sim.setArriveTime(DateUtils.dateToStr(DateUtils.addMonth(today, Integer.parseInt(norm.getDeadline()))));
                    }
                }
                simDxMapper.updateById(sim);
            }
        }

        return R.ok();
    }


    /**
     * 测试
     *
     * @return
     */
    @RequestMapping("/test")
    @ResponseBody
    //@Scheduled(cron = "0/5 * * * * *")
    // @Scheduled(fixedRate = 5000)
    public Object test() {

        //System.out.println("=====Scheduled=>" + System.currentTimeMillis());
        return R.ok();
    }


}
